package jsqlite;

import java.io.PrintStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/* JADX INFO: Access modifiers changed from: package-private */
/* compiled from: Benchmark.java */
/* loaded from: classes2.dex */
public class BenchmarkThread extends Thread {
    Connection Conn;
    Benchmark bench;
    int ntrans;
    PreparedStatement pstmt1;
    PreparedStatement pstmt2;
    PreparedStatement pstmt3;
    PreparedStatement pstmt4;
    PreparedStatement pstmt5;

    public BenchmarkThread(int i, String str, String str2, String str3, Benchmark benchmark) {
        this.ntrans = 0;
        this.pstmt1 = null;
        this.pstmt2 = null;
        this.pstmt3 = null;
        this.pstmt4 = null;
        this.pstmt5 = null;
        this.bench = benchmark;
        this.ntrans = i;
        Connection connect = benchmark.connect(str, str2, str3);
        this.Conn = connect;
        if (connect == null) {
            return;
        }
        try {
            if (Benchmark.transactions) {
                this.Conn.setAutoCommit(false);
            }
            if (Benchmark.prepared_stmt) {
                this.pstmt1 = this.Conn.prepareStatement(String.valueOf("UPDATE accounts SET Abalance = Abalance + ?") + " WHERE Aid = ?");
                this.pstmt2 = this.Conn.prepareStatement(String.valueOf("SELECT Abalance FROM accounts") + " WHERE Aid = ?");
                this.pstmt3 = this.Conn.prepareStatement(String.valueOf("UPDATE tellers SET Tbalance = Tbalance + ?") + " WHERE  Tid = ?");
                this.pstmt4 = this.Conn.prepareStatement(String.valueOf("UPDATE branches SET Bbalance = Bbalance + ?") + " WHERE  Bid = ?");
                this.pstmt5 = this.Conn.prepareStatement("INSERT INTO history(Tid, Bid, Aid, delta) VALUES (?,?,?,?)");
            }
        } catch (java.lang.Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }

    @Override // java.lang.Thread, java.lang.Runnable
    public void run() {
        while (true) {
            int i = this.ntrans;
            this.ntrans = i - 1;
            if (i <= 0) {
                break;
            }
            doOne(Benchmark.getRandomID(1), Benchmark.getRandomID(0), Benchmark.getRandomID(2), Benchmark.getRandomInt(0, 1000));
            this.bench.incrementTransactionCount();
        }
        if (Benchmark.prepared_stmt) {
            try {
                PreparedStatement preparedStatement = this.pstmt1;
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                PreparedStatement preparedStatement2 = this.pstmt2;
                if (preparedStatement2 != null) {
                    preparedStatement2.close();
                }
                PreparedStatement preparedStatement3 = this.pstmt3;
                if (preparedStatement3 != null) {
                    preparedStatement3.close();
                }
                PreparedStatement preparedStatement4 = this.pstmt4;
                if (preparedStatement4 != null) {
                    preparedStatement4.close();
                }
                PreparedStatement preparedStatement5 = this.pstmt5;
                if (preparedStatement5 != null) {
                    preparedStatement5.close();
                }
            } catch (java.lang.Exception e) {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }
        Benchmark.connectClose(this.Conn);
        this.Conn = null;
    }

    int doOne(int i, int i2, int i3, int i4) {
        int i5;
        if (this.Conn == null) {
            this.bench.incrementFailedTransactionCount();
            return 0;
        }
        try {
            if (Benchmark.prepared_stmt) {
                this.pstmt1.setInt(1, i4);
                this.pstmt1.setInt(2, i3);
                this.pstmt1.executeUpdate();
                this.pstmt1.clearWarnings();
                this.pstmt2.setInt(1, i3);
                ResultSet executeQuery = this.pstmt2.executeQuery();
                this.pstmt2.clearWarnings();
                i5 = 0;
                while (executeQuery.next()) {
                    i5 = executeQuery.getInt(1);
                }
                this.pstmt3.setInt(1, i4);
                this.pstmt3.setInt(2, i2);
                this.pstmt3.executeUpdate();
                this.pstmt3.clearWarnings();
                this.pstmt4.setInt(1, i4);
                this.pstmt4.setInt(2, i);
                this.pstmt4.executeUpdate();
                this.pstmt4.clearWarnings();
                this.pstmt5.setInt(1, i2);
                this.pstmt5.setInt(2, i);
                this.pstmt5.setInt(3, i3);
                this.pstmt5.setInt(4, i4);
                this.pstmt5.executeUpdate();
                this.pstmt5.clearWarnings();
            } else {
                Statement createStatement = this.Conn.createStatement();
                StringBuilder sb = new StringBuilder(String.valueOf("UPDATE accounts SET Abalance = Abalance + " + i4));
                sb.append(" WHERE Aid = ");
                sb.append(i3);
                createStatement.executeUpdate(sb.toString());
                createStatement.clearWarnings();
                ResultSet executeQuery2 = createStatement.executeQuery(String.valueOf("SELECT Abalance FROM accounts") + " WHERE Aid = " + i3);
                createStatement.clearWarnings();
                int i6 = 0;
                while (executeQuery2.next()) {
                    i6 = executeQuery2.getInt(1);
                }
                StringBuilder sb2 = new StringBuilder(String.valueOf("UPDATE tellers SET Tbalance = Tbalance + " + i4));
                sb2.append(" WHERE Tid = ");
                sb2.append(i2);
                createStatement.executeUpdate(sb2.toString());
                createStatement.clearWarnings();
                StringBuilder sb3 = new StringBuilder(String.valueOf("UPDATE branches SET Bbalance = Bbalance + " + i4));
                sb3.append(" WHERE Bid = ");
                sb3.append(i);
                createStatement.executeUpdate(sb3.toString());
                createStatement.clearWarnings();
                StringBuilder sb4 = new StringBuilder(String.valueOf(String.valueOf("INSERT INTO history(Tid, Bid, Aid, delta) VALUES (") + i2 + ","));
                sb4.append(i);
                sb4.append(",");
                StringBuilder sb5 = new StringBuilder(String.valueOf(String.valueOf(sb4.toString()) + i3 + ","));
                sb5.append(i4);
                sb5.append(")");
                createStatement.executeUpdate(sb5.toString());
                createStatement.clearWarnings();
                createStatement.close();
                i5 = i6;
            }
            if (Benchmark.transactions) {
                this.Conn.commit();
            }
            return i5;
        } catch (java.lang.Exception e) {
            if (Benchmark.verbose) {
                PrintStream printStream = System.out;
                printStream.println("Transaction failed: " + e.getMessage());
                e.printStackTrace();
            }
            this.bench.incrementFailedTransactionCount();
            if (Benchmark.transactions) {
                try {
                    this.Conn.rollback();
                } catch (SQLException unused) {
                }
            }
            return 0;
        }
    }
}
